<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "@/dashboard/mixins/resize";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "520px",
    },
    autoResize: {
      type: Boolean,
      default: true,
    },
    chartData: {
      type: Object,
      required: true,
    },
    xAxis: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      chart: null,
      xAxis: this.getXData("2014/12", 84),
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val);
      },
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      this.setOptions(this.chartData);
    },
    setOptions(chartData) {

      this.chart.setOption({
        title: {
          text: chartData.seriesName + "销量预测",
          link: `https://price.pcauto.com.cn/${chartData.seriesId}/config.html`,
          x: "center",
          y: "top",
          subtext: "——基于SARIMA模型的销量预测",
          subtextStyle: {
            color: "#000000",
          },
          textStyle: {
            fontSize: "24",
            color: "#283618",
          },
        },
        xAxis: {
          data: this.xAxis,
          boundaryGap: false,
          axisTick: {
            show: false,
          },
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 30,
          containLabel: true,
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
          },
          padding: [5, 10],
        },
        yAxis: {
          axisTick: {
            show: false,
          },
        },
        legend: {
          data: ["真实销量", "滚动预测销量", "传统预测销量"],
          right: 0,
        },
        series: [
          {
            name: "真实销量",
            itemStyle: {
              normal: {
                color: "#283618",
                lineStyle: {
                  color: "#283618",
                  width: 2,
                  type: "dashed",
                },
              },
            },
            smooth: true,
            type: "line",
            data: chartData.salesData,
            animationDuration: 2800,
            animationEasing: "cubicInOut",
          },
          {
            name: "滚动销量预测",
            smooth: true,
            type: "line",
            itemStyle: {
              normal: {
                color: "#FF005A",
                lineStyle: {
                  color: "#FF005A",
                  width: 2,
                },
                areaStyle: {
                  color: "#f3f8ff",
                },
              },
            },
            data: chartData.rollPredRes,
            animationDuration: 2800,
            animationEasing: "quadraticOut",
          },
          {
            name: "传统销量预测",
            smooth: true,
            type: "line",
            itemStyle: {
              normal: {
                color: "#3888FA",
                lineStyle: {
                  color: "#3888FA",
                  width: 2,
                },
              },
            },
            data: chartData.traditionPredRes,
            animationDuration: 2800,
            animationEasing: "quadraticOut",
          },
        ],
      });
    },
  },
};
</script>
